package com.map.struct.bean.convert;

import com.map.struct.bean.dto.CarDTO;
import com.map.struct.bean.dto.DriverDTO;
import com.map.struct.bean.dto.PartDTO;
import com.map.struct.bean.vo.CarVO;
import com.map.struct.bean.vo.DriverVO;
import com.map.struct.bean.vo.VehicleVO;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 1、引入依赖 版本注意<lombok.version>1.18.10</lombok.version>
 *         <mapstruct.version>1.3.0.Final</mapstruct.version>
 * 2.新建一个抽象类或者接口并标注@Mapper
 * 3.写一个转换方法  方法名字是任意的
 * 4.获取对象INSTANCE并使用
 */
@Mapper(componentModel = "spring")
public interface CarConvert {

    public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class);

    /**
     * carDTO--->carVO
     */
    @Mappings(value = {
            @Mapping(source = "totalPrice", target = "totalPrice", numberFormat = "#.00"),
            @Mapping(source = "publishDate", target = "publishDate", dateFormat = "yyyy-MM-dd HH:mm:ss"),
            @Mapping(target = "color", ignore = true),
            @Mapping(source = "brand", target = "brandName"),
            @Mapping(source = "driverDTO", target = "driverVO"),
    })
    public CarVO carDTO2CarVO(CarDTO carDTO);

    @Mappings(value = {
            @Mapping(source = "id", target = "driverId"),
            @Mapping(source = "name", target = "fullName"),
    })
    public DriverVO driverDTO2DriverVO(DriverDTO driverDTO);

    @AfterMapping
    default public void dto2voAfter(CarDTO carDTO, @MappingTarget CarVO carVO) {
        List<PartDTO> partDTOs = carDTO.getPartDTOs();
        carVO.setHasPart(partDTOs != null && !partDTOs.isEmpty());
    }

    public List<CarVO> dto2vos(List<CarDTO> carDTOS);

    /**
     * BeanMapping 忽略mapstruct的默认映射行为，避免不需要的赋值，
     * 避免属性覆盖
     * @param carDTO
     * @return
     */
    @BeanMapping(ignoreByDefault = true)
    @Mapping(source = "id",target = "id")
    @Mapping(source = "brand",target = "brandName")
    public VehicleVO carDTO2VehicleVO(CarDTO carDTO);

    /**
     * InheritConfiguration
     * 更新的场景，避免同样的配置写多份
     * @param carDTO
     * @param vehicleVO
     */
    @InheritConfiguration
    @Mapping(target = "id",ignore = true)
    public void updateVechicleVO(CarDTO carDTO,@MappingTarget VehicleVO vehicleVO);

    /**
     * 反向映射不用反过来再写一遍，注意：只继承@Mapping注解配置，不会继承
     * @param vehicleVO
     * @return
     */
    @BeanMapping(ignoreByDefault = true)
    @InheritInverseConfiguration(name = "carDTO2VehicleVO")
    public CarDTO vechicleVO2CarDTO(VehicleVO vehicleVO);
}
